﻿using System;
using System.Windows.Forms;
using DosTexPrj.Core.RepositoriesInterface;
using DosTexPrj.Implement.NHibernate;
using DosTexPrj.UI.WinForm.Commons;
using DosTexPrj.Core.Models;

namespace DosTexPrj.UI.WinForm
{
    public partial class FabricTypeForm : Form
    {
        #region --- Private members ---

        private IRepository _repository;

        #endregion --- Private members ---

        #region --- Constructors ---

        public FabricTypeForm()
        {
            InitializeComponent();
            _repository = MyServiceLocator.DoGetInstance<IRepository>();
        }

        #endregion --- Constructors ---

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValidate())
                return;
            FabricType fabricType = new FabricType
            {
                FabricTypeName = txtName.Text.Trim()
            };

            try
            {
                this._repository.Save<FabricType>(fabricType);
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearForm();
        }

        #region Function

        private void ClearForm()
        {
            // Clear textboxes
            txtName.Text = string.Empty;
        }

        private bool CheckValidate()
        {
            error.Clear();
            bool flag = true;
            if (!Common.IsBlankTextBox(txtName, "Required field!", error))
                flag = false;
            return flag;
        }

        #endregion Functions
    }
}